package com.andong.aop;

import com.andong.domain.entity.Log;
import com.andong.mapper.LogMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>ClassName: LogAspect</p>
 *
 * @author XHU_WHY
 * @version 1.0
 * @since 2024/5/13 15:13
 */
@Component
@Aspect
public class LogAspect {
    @Resource
    private LogMapper logMapper;
    @Resource
    private HttpServletRequest request;
    @Around("execution(* com.andong.controller.*.*(..))")
    public Object recordLog(ProceedingJoinPoint joinPoint) throws Throwable{
        // 记录开始时间
        long start = System.currentTimeMillis();
        Object o = joinPoint.proceed();
        // 记录结束时间
        long end = System.currentTimeMillis();
        // 操作的类名
        String className = joinPoint.getTarget().getClass().getName();
        // 操作的方法名
        String methodName = joinPoint.getSignature().getName();
        // 请求的url
        String url = request.getRequestURL().toString();
        Log log = new Log();
        log.setType(className + "." + methodName);
        log.setTime((int)(end - start));
        log.setUrl(url);
        logMapper.insert(log);
        return o;
    }
}
